package com.tsugun.mediaplayer.view.adapter;

import android.content.Context;
import android.support.v4.media.session.PlaybackStateCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.tsugun.mediaplayer.R;
import com.tsugun.mediaplayer.model.entity.MusicModel;
import com.tsugun.mediaplayer.presenter.service.MusicAudioService;
import com.tsugun.mediaplayer.presenter.utils.SettingPreferenceHelper;
import com.tsugun.mediaplayer.view.loader.MediaImageLoad;

import java.util.List;

/**
 * 播放列队项目适配器
 * Created by shize on 2017/5/22.
 */

public class PlayListItemAdapter extends RecyclerView.Adapter<PlayListItemAdapter.PlayListViewHolder> {

    private LayoutInflater mInflater;
    private List<MusicModel> mData;
    private OnPlayListClickListener mClickListener;

    private RecyclerView mRv;

    private int mCurrent = -1; // 当前播放的位置
    private int mThemeColor; // 主题色

    public PlayListItemAdapter(Context context, List<MusicModel> musicList) {
        mInflater = LayoutInflater.from(context);
        mData = musicList;
        currentPositionOnList();
        mThemeColor = SettingPreferenceHelper.INSTANCE.getThemeColorPreference(context);
    }

    /**
     * 当前位置
     */
    private void currentPositionOnList() {
        if (MusicAudioService.Companion.getMState() != PlaybackStateCompat.STATE_STOPPED) {
            mCurrent = MusicAudioService.Companion.getMCurrentPosition();
            return;
        }
        mCurrent = -1;
    }

    /**
     * 注册点击事件
     *
     * @param listener 点击事件
     */
    public void setOnPlayListClickListener(OnPlayListClickListener listener) {
        mClickListener = listener;
    }

    @Override
    public PlayListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new PlayListViewHolder(mInflater.inflate(R.layout.item_play_list, parent, false));
    }

    @Override
    public void onBindViewHolder(final PlayListViewHolder holder, int position) {
        MusicModel musicModel = mData.get(position);
        holder.mIvAlbum.setImageResource(R.drawable.ic_music_default);
        MediaImageLoad.localImageLoad(musicModel, holder.mIvAlbum);
        holder.mTvTitle.setText(musicModel.getTitle());
        holder.mTvSubTitle.setText(musicModel.getArtist());
        if (position == mCurrent) {
            holder.mCurrent.setVisibility(View.VISIBLE);
        } else {
            holder.mCurrent.setVisibility(View.GONE);
        }

        if (mClickListener == null) {
            return;
        }
        holder.mIbDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mClickListener.onDeleteClick(holder.getAdapterPosition());
                if (holder.getAdapterPosition() < mCurrent) {
                    mCurrent--;
                }
            }
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mClickListener.onItemClick(holder.getAdapterPosition());
            }
        });

        setSettingTheme(holder);
    }

    // 设置主题色
    private void setSettingTheme(PlayListViewHolder holder) {
        holder.mCurrent.setBackgroundColor(mThemeColor);
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        mRv = recyclerView;
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    /**
     * 更新列表的item
     */
    public void updateListItem() {
        if (mRv == null) {
            return;
        }
        int oldPosition = mCurrent;
        currentPositionOnList();
        notifyItemChanged(oldPosition);
        notifyItemChanged(mCurrent);
        mRv.scrollToPosition(mCurrent);
    }

    class PlayListViewHolder extends RecyclerView.ViewHolder {

        private ImageView mIvAlbum;
        private TextView mTvTitle;
        private TextView mTvSubTitle;
        private ImageButton mIbDelete;
        private View mCurrent;

        PlayListViewHolder(View itemView) {
            super(itemView);
            mIvAlbum = (ImageView) itemView.findViewById(R.id.id_item_play_list_image);
            mTvTitle = (TextView) itemView.findViewById(R.id.id_item_play_list_title);
            mTvSubTitle = (TextView) itemView.findViewById(R.id.id_item_play_list_sub_title);
            mIbDelete = (ImageButton) itemView.findViewById(R.id.id_item_play_list_delete);
            mCurrent = itemView.findViewById(R.id.id_item_play_list_current);
        }
    }

    public interface OnPlayListClickListener {
        /**
         * 项目点击事件
         *
         * @param position 项目位置
         */
        void onItemClick(int position);

        /**
         * 删除按钮点击事件
         *
         * @param position 项目位置
         */
        void onDeleteClick(int position);
    }

}
